package com.yiwei.zhh.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.yiwei.zhh.R;
import com.yiwei.zhh.api.callback.EmailContract;
import com.yiwei.zhh.api.requestInfo.EmailRequest;
import com.yiwei.zhh.constant.Const;
import com.yiwei.zhh.model.ResponseModel;
import com.yiwei.zhh.util.ToastUtil;
import com.yiwei.zhh.util.UI;
import com.yiwei.zhh.util.Util;
import com.yiwei.zhh.view.CircularProgress;
import com.yiwei.zhh.view.Dialog;
import com.yiwei.zhh.view.LastInputEditText;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 2017/5/12.
 */
// 邮箱认证
public class EmailIdentityActivity extends BaseActivity implements EmailContract.View {


    @BindView(R.id.edit_email)
    EditText editEmail;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    @BindView(R.id.progressBar)
    CircularProgress progressBar;
    private EmailRequest api;
    private boolean isIdentity = false;
    private Unbinder unbiner;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentView(R.layout.activity_identity_email_v2);
        baseInitView(this, "EmailIdentityActivity", Const.ViewType.ACTIVITY);
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        setBackIntent();
        setTitleText("邮箱认证");
        String isident = getIntent().getStringExtra("isIdentity");
        if (!TextUtils.isEmpty(isident)) {
            if (isident.equals("0")) {
                isIdentity = false;
            } else {
                isIdentity = true;
                editEmail.setEnabled(false);
                editEmail.setText(getIntent().getStringExtra("email"));
            }
        }
        api = EmailRequest.create();
        api.attachView(this);


//        如果email通过的话，就不显示和禁止输入
        if (isIdentity) {
            btnSubmit.setText("已通过认证");
            btnSubmit.setBackgroundResource(R.drawable.item_nickname_btn_grey);
        } else {
            editEmail.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (TextUtils.isEmpty(UI.toString(editEmail))) {
                        btnSubmit.setBackgroundResource(R.drawable.item_nickname_btn_grey);
                    } else {
                        btnSubmit.setBackgroundResource(R.drawable.item_nickname_btn);
                    }
                }
            });
        }
    }


    @Override
    public void showError() {

    }

    @Override
    public void complete() {
        showLoading(false);
    }

    @Override
    public void onRequestStart() {
        showLoading(true);
    }

    @Override
    public void getEmailSuccess(ResponseModel item) {
        btnSubmit.setText("已发送验证邮件");
//        btnSubmit.setBackgroundResource(R.drawable.btn_grey_bg);
//        btnSubmit.setEnabled(false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        api.detachView();
        unbiner.unbind();
    }


    private void goToGetData() {
        if (checkEmail()) {
            Dialog.showDefaultDialog(EmailIdentityActivity.this, "提示", "系统将向" + Util.toString(editEmail) + "发送验证邮件，请于12小时内进行验证", "取消", "确定", false, new Dialog.DialogDefaultClickListener() {
                @Override
                public void confirm() {
                    api.getEmail(Util.toString(editEmail), "add");
                }

                @Override
                public void cancel() {

                }
            });
        }
    }

    /**
     * 是否显示加载框
     *
     * @param b
     */
    private void showLoading(boolean b) {
        if (b) {
            btnSubmit.setEnabled(false);
            if (progressBar != null) progressBar.setVisibility(View.VISIBLE);
        } else {
            btnSubmit.setEnabled(true);
            if (progressBar != null) progressBar.setVisibility(View.GONE);
        }
    }


    /**
     * 邮箱验证
     *
     * @return
     */
    private String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";

    private boolean checkEmail() {
        if (TextUtils.isEmpty(editEmail.getText().toString())) {
            ToastUtil.makeText(this, "邮箱不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(editEmail.getText().toString());
        return matcher.matches();
    }

    @OnClick({R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_submit:
                if (isIdentity) {
                    return;
                }
                goToGetData();
                break;
        }
    }
}
